<?php
// 设置会话参数以保持一个月有效期
$sessionLifetime = 60 * 60 * 24 * 30; // 30天
ini_set('session.gc_maxlifetime', $sessionLifetime); // 服务器端会话有效期
session_start(); // 启用会话管理

$errors = []; // 定义错误提示数组

// 引入安全模块
require_once 'login_security.php';
require_once 'ban_system.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    $agreement = $_POST['agreement'] ?? '';

    // 验证是否同意公约
    if ($agreement !== 'on') {
        $errors[] = "请阅读并同意隐私条款";
    } else {
        // 检查登录尝试限制
        $lockError = checkLoginAttempts($username);
        if ($lockError) {
            $errors[] = $lockError;
        } 
        // 检查用户是否被封禁
        elseif ($banInfo = checkBanOnLogin($username)) {
            $errors[] = "您的账号已被封禁! 理由: {$banInfo['reason']} 结束时间: {$endTime}";
        }
        // 验证用户名是否存在
        elseif (!is_dir("user/$username")) {
            $errors[] = "用户不存在，请先注册！";
        } else {
            // 验证密码
            $user_info_file = "user/$username/info.json";
            if (!file_exists($user_info_file)) {
                $errors[] = "用户数据异常，请联系管理员！";
            } else {
                // 读取用户信息
                $user_data = json_decode(file_get_contents($user_info_file), true);
                if (!password_verify($password, $user_data['password'])) {
                    recordFailedAttempt($username); 
                    $errors[] = "密码错误，请重新输入！";
                }
            }
        }
    }

    // 如果没有错误，登录成功，跳转到主页
    if (empty($errors)) {
        // 清除失败记录
        $lockFile = 'login_locks.json';
        if (file_exists($lockFile)) {
            $locks = json_decode(file_get_contents($lockFile), true);
            if (isset($locks[$username])) {
                unset($locks[$username]);
                file_put_contents($lockFile, json_encode($locks));
            }
        }
        
        $_SESSION['username'] = $username; // 保存用户名到会话
        header("Location: index.php"); // 跳转到主页
        exit;
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录 - JIILCHAT</title>
    <meta name="description" content="JIILchat,全新的即时聊天系统">
    <link rel="icon" href="/favicon.ico" type="image/x-icon">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
        }
        
        body {
            background-color: #ffffff;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            justify-content: center;
            padding: 20px;
        }
        
        .login-container {
            display: flex;
            flex-direction: column;
            width: 100%;
            max-width: 500px;
            margin: 0 auto;
            padding: 40px 30px;
            background: white;
            border-radius: 16px;
            box-shadow: 0 10px 40px rgba(0, 0, 0, 0.08);
        }
        
        .login-header {
            text-align: center;
            margin-bottom: 40px;
        }
        
        .app-name {
            font-size: 36px;
            font-weight: 700;
            color: #333;
            margin-bottom: 10px;
            letter-spacing: 1px;
        }
        
        .app-desc {
            font-size: 18px;
            color: #666;
            font-weight: 400;
        }
        
        .login-form {
            width: 100%;
        }
        
        .form-group {
            margin-bottom: 25px;
        }
        
        .form-label {
            display: block;
            margin-bottom: 10px;
            font-size: 16px;
            color: #333;
            font-weight: 500;
        }
        
        .form-control {
            width: 100%;
            padding: 16px 18px;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            font-size: 16px;
            transition: all 0.25s ease;
            background: #f9f9f9;
            color: #333;
        }
        
        .form-control:focus {
            outline: none;
            border-color: #4a90e2;
            background: #fff;
            box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.15);
        }
        
        .privacy-notice {
            background: #f8f9fa;
            border-radius: 8px;
            padding: 20px;
            margin: 30px 0;
            text-align: center;
            border: 1px solid #e9ecef;
        }
        
        .privacy-notice-text {
            font-size: 16px;
            color: #555;
            line-height: 1.6;
        }
        
        .privacy-emphasis {
            font-weight: 600;
            color: #e74c3c;
        }
        
        .agreement-container {
            margin: 20px 0;
            display: flex;
            align-items: flex-start;
        }
        
        .agreement-checkbox {
            margin-right: 12px;
            margin-top: 3px;
            width: 20px;
            height: 20px;
            accent-color: #4a90e2;
        }
        
        .agreement-text {
            font-size: 15px;
            color: #555;
            line-height: 1.5;
        }
        
        .agreement-link {
            color: #4a90e2;
            text-decoration: none;
            font-weight: 500;
            cursor: pointer;
            border-bottom: 1px solid #a3bffa;
        }
        
        .agreement-link:hover {
            color: #357ae8;
            border-bottom: 1px solid #4a90e2;
        }
        
        .login-btn {
            width: 100%;
            padding: 16px;
            background: #4a90e2;
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 18px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.25s ease;
        }
        
        .login-btn:hover {
            background: #357ae8;
        }
        
        .links-container {
            display: flex;
            justify-content: space-between;
            margin-top: 30px;
            padding-top: 20px;
            border-top: 1px solid #f0f0f0;
        }
        
        .footer-link {
            color: #4a90e2;
            text-decoration: none;
            font-size: 16px;
            font-weight: 500;
        }
        
        .footer-link:hover {
            text-decoration: underline;
        }
        
        .error-messages {
            background: #ffebee;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 25px;
            font-size: 15px;
            color: #c62828;
        }
        
        .error-messages ul {
            padding-left: 20px;
        }
        
        .error-messages li {
            margin-bottom: 5px;
        }
        
        /* 响应式设计 */
        @media (max-width: 768px) {
            .login-container {
                padding: 30px 20px;
            }
            
            .app-name {
                font-size: 32px;
            }
            
            .app-desc {
                font-size: 16px;
            }
        }
        
        @media (max-width: 480px) {
            .login-container {
                padding: 25px 15px;
            }
            
            .app-name {
                font-size: 28px;
            }
            
            .app-desc {
                font-size: 15px;
            }
            
            .form-control {
                padding: 14px 16px;
                font-size: 15px;
            }
            
            .privacy-notice {
                padding: 15px;
            }
            
            .privacy-notice-text {
                font-size: 15px;
            }
            
            .agreement-text {
                font-size: 14px;
            }
            
            .login-btn {
                font-size: 16px;
                padding: 15px;
            }
        }
    </style>
</head>
<body>
    <!-- 登录容器 -->
    <div class="login-container">
        <!-- 登录头部 -->
        <div class="login-header">
            <div class="app-name">JIIL微聊</div>
            <div class="app-desc">更高效的即时聊天服务</div>
        </div>
        
        <!-- 错误提示 -->
        <?php if (!empty($errors)): ?>
            <div class="error-messages">
                <ul>
                    <?php foreach ($errors as $error): ?>
                        <li><?php echo $error; ?></li>
                    <?php endforeach; ?>
                </ul>
            </div>
        <?php endif; ?>
        
        <!-- 登录表单 -->
        <div class="login-form">
            <form method="POST">
                <div class="form-group">
                    <label for="username" class="form-label">用户名</label>
                    <input type="text" id="username" name="username" class="form-control" placeholder="请输入用户名" required>
                </div>
                
                <div class="form-group">
                    <label for="password" class="form-label">密码</label>
                    <input type="password" id="password" name="password" class="form-control" placeholder="请输入密码" required>
                </div>
                
                <!-- 隐私政策提示 -->
                <div class="privacy-notice">
                    <p class="privacy-notice-text">
                        我们的隐私政策已经写得<span class="privacy-emphasis">非常清晰易懂</span>，
                        请您务必<span class="privacy-emphasis">完整阅读</span>。
                        若因未仔细阅读而导致的问题，我们将<span class="privacy-emphasis">概不负责</span>。
                    </p>
                </div>
                
                <!-- 在同意条款区域添加链接 -->
                <div class="agreement-container">
                    <input type="checkbox" id="agreement" name="agreement" class="agreement-checkbox" required>
                    <label for="agreement" class="agreement-text">
                        我已阅读并同意
                        <a href="ysbh.html" target="_blank" class="agreement-link">《隐私条款》</a>
                    </label>
                </div>
                
                <button type="submit" class="login-btn">登录</button>
            </form>
            
            <div class="links-container">
                <a href="register.php" class="footer-link">注册新账号</a>
            </div>
        </div>
    </div>
</body>
</html>